package com.resume.utils;

import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 博客工具类
 *
 * @author 梁云亮
 */
public class ResumeUtil {
    /**
     * 字符串首字母大写
     *
     * @param str
     * @return
     */
    public static String firstUpper(String str) {
        //     str = str.substring(0, 1).toUpperCase() + str.substring(1);
        //    return  str;
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }

    public static void main(String[] args) {
        //System.out.println(firstUpper("zhangsan"));
    }

    /**
     * 字节数组转16进制字符串
     *
     * @param data 待转换的字节数组
     * @return 转换后得到的16进制字符串
     */
    public String byte2string(byte[] data) {
        if (data == null || data.length <= 1) {
            return "0x";
        }
        if (data.length > 200000) {
            return "0x";
        }
        StringBuffer sb = new StringBuffer();
        int buf[] = new int[data.length];

        for (int k = 0; k < data.length; k++) { //byte数组转化成十进制
            buf[k] = data[k] < 0 ? (data[k] + 256) : (data[k]);
        }
        //十进制转化成十六进制
        for (int k = 0; k < buf.length; k++) {
            if (buf[k] < 16) {
                sb.append("0" + Integer.toHexString(buf[k]));
            } else {
                sb.append(Integer.toHexString(buf[k]));
            }
        }
        return "0x" + sb.toString().toUpperCase();
    }

    /**
     * 字节数组转换为图片
     *
     * @param data 待转换的字节数组
     * @param path 生成的图片的名称（带路径）
     */
    public void byteArray2img(byte[] data, String path) {
        if (data.length < 3 || "".equals(path)) {
            return;
        }
        try (FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));) {
            imageOutput.write(data, 0, data.length);
        } catch (Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }

    /**
     * 图片转字节数组
     *
     * @param path
     * @return
     */
    public byte[] img2byteArray(String path) {
        try (FileImageInputStream input = new FileImageInputStream(new File(path));
             ByteArrayOutputStream output = new ByteArrayOutputStream();) {
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            return output.toByteArray();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转换为InputStream
     *
     * @param str
     * @return
     */
    public InputStream string2InputStream(String str) {
        ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
        return stream;
    }

    /**
     * 字节流转为字符流
     *
     * @param is
     * @return
     * @throws Exception
     */
    public String inputStream2String(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        return buffer.toString();
    }
}
